// JavaScript Document
var categoryDTOS;
$(function(){
    var param = {};
    param.id = getQueryVariable("expenseApplyId");
    var dataMap = doAjax(get_expense_apply, param);
    var expenseApply = dataMap.parent;
    var times=new Date(expenseApply.createTime).pattern("yyyy-MM-dd hh:mm:ss");
    // 设置父报销数据
    $(".header ul li:eq(0) span").html(expenseApply.code);
    $(".header ul li:eq(1) span").html(expenseApply.goodsName);
    $(".header ul li:eq(2) span").html(expenseApply.applyUserName);
    $(".header ul li:eq(3) span").html(times);
    $(".header ul li:eq(4) span").html(expenseApply.expenseStoreName);
    $(".header ul li:eq(5) span").html(expenseApply.operateUserName);
    //报销申请
    var initData = doAjaxOnlyUrl(expense_apply_init_url);
    categoryDTOS = initData.categoryDTOS;
    var goodsSelectOneStr = "<select onchange='selectOne(this)'><option>一级菜单</option>";
    var goodsSelectTwoStr = "<select ><option>二级菜单</option></select>";
    for (var i = 0; i < categoryDTOS.length; i++){
        goodsSelectOneStr = goodsSelectOneStr + "<option value="+categoryDTOS[i].id+">"+categoryDTOS[i].name+"</option>";
    }
    goodsSelectOneStr = goodsSelectOneStr + "</select>";
    //报销申请
    $('.add-btn').click(function(){
        var tr ="<tr name='expenseData'>"+
            "<td class=\"txt\">"+goodsSelectOneStr+goodsSelectTwoStr+"</td>"+
            "<td class=\"txt\"><input></td>"+
            "<td class=\"txt\">"+expenseApply.expenseStoreName+"</td>"+
            "<td class=\"txt\"><input></td>"+
            "<td class=\"txt\"><a href=\"javascript:;\" onclick=\"deleteLine(this)\">删除<input></td>"+
            "</tr>"
        $('#Apply').append(tr);

    });
    /*
    var children = dataMap.children;
    if(children && children.length != 0){
        // 如果有数据，则加载页面信息
        $.each(children, function (index, value) {
            var tr ="<tr>"+
                "<td class=\"txt\">"+value.content+"</td>"+
                "<td class=\"txt\">"+value.number+"</td>"+
                "<td class=\"txt\">"+value.amount+"</td>"+
                "<td class=\"txt\">否</td>"+
                "</tr>"
            $('#Apply').append(tr);
        });
    }*/

    // 确认报销
    $(".rit-btn").click(function () {
        if(confirm("确认提交追加报销申请吗") == true){
            var applyList = new Array();
            var trs = $("tr[name='expenseData']");
            if(trs.length == 0){
                alert("请填写报销数据");
                return;
            }
            var flag = 0;
            trs.each(function (i) {
                var obj = {};
                console.log($(this).children('td').eq(0).find('option:selected').text());
                obj.goodsName = $(this).children('td').eq(0).children('select').eq(1).find("option:selected").text();
                if(obj.goodsName == "二级菜单"){
                    flag = 1;
                    alert("请选择报销名称");
                    return;
                }
                obj.price = $(this).children('td').eq(1).children('input').val();
                if(obj.price == null || obj.price.trim() == ''){
                    flag = 1;
                    alert("金额不能为空");
                    return;
                }
                var remark = $(this).children('td').eq(3).children('input').val();
                if(remark.length > 30){
                    flag = 1;
                    alert("备注不能超过30个字符");
                    return false;
                }
                obj.remark = remark;
                applyList[i] = obj;
            });
            if(flag){
                return;
            }
            var data = {};
            data.parentId = getQueryVariable("expenseApplyId");
            data.applyList = JSON.stringify(applyList);
            var result = doAjaxAll(add_expensy_apply, data);
            if(result.code == 1){
                alert("追加申请成功！");
                location.reload();
            }
        }
    });
});
function selectOne(obj) {
    var id = $(obj).val();
    var selectTwo = $(obj).next();
    var categoryDTOTmp;
    for (var i = 0; i < categoryDTOS.length; i++){
        if (categoryDTOS[i].id == id){
            categoryDTOTmp = categoryDTOS[i].categoryDTOS;
            break;
        }
    }
    selectTwo.empty();
    selectTwo.prepend("<option>二级菜单</option>")
    for (var i = 0; i < categoryDTOTmp.length; i++){
        selectTwo.append("<option value="+categoryDTOTmp[i].id+">"+categoryDTOTmp[i].name+"</option>");
    }
}
function deleteLine(obj) {
    $(obj).parent().parent().remove();
}

























